package com.lsh.controller;

import com.lsh.constants.MessageConstant;
import com.lsh.entity.PageResult;
import com.lsh.entity.QueryPageBean;
import com.lsh.entity.Result;
import com.lsh.pojo.CheckGroup;
import com.lsh.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;
    /**
     * 添加检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_HEALTH_MANAGER')")
    public Result add(@RequestBody CheckGroup checkGroup, @RequestParam("checkitemIds") Integer[] checkitemIds){
        checkGroupService.add(checkGroup,checkitemIds);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 分页查询
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @PostMapping("/findpage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);
    }

    /**
     * 通过检查组id查询该检查组
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @GetMapping("/findbyid")
    public Result findById(@RequestParam("checkGroupId") Integer id){
        CheckGroup checkGroup = checkGroupService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }

    /**
     * 通过检查组id查询该组下的检查项列表
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @GetMapping("/findCheckitemIdsByCheckGroupId")
    public Result findCheckitemIdsByCheckGroupId(@RequestParam("checkGroupId") Integer id){
        List<Integer> integerList = checkGroupService.findCheckitemIdsByCheckGroupId(id);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_CHECKITEM_SUCCESS,integerList.toArray(new Integer[integerList.size()]));
    }

    /**
     * 更新检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/update")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_HEALTH_MANAGER')")
    public Result update(@RequestBody CheckGroup checkGroup,@RequestParam("checkitemIds") Integer[] checkitemIds){
        checkGroupService.update(checkGroup,checkitemIds);
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 删除检查组
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_HEALTH_MANAGER')")
    public Result deleteById(@RequestParam("id") Integer id){
        checkGroupService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroupList = checkGroupService.findAll();
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
    }
}
